﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace CrazyClient
{
    public class AssetHandler
    {
        public AssetHandler()
        {
            _assets = new AssetInfoList();
        }
        public static string BaseFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "//CrazyWorship";
        private string assetPath;
        public void Start(string uri)
        {
            assetPath = BaseFolder + "\\Client\\" + uri.Replace("http://", "").Replace("/", "").Replace("\\", "").Replace(":","")+"\\";
            LoadAssetInfo();
        }
        private void LoadAssetInfo()
        {
            _assets.Clear();
            if (!Directory.Exists(assetPath))
                Directory.CreateDirectory(assetPath);
            foreach (DirectoryInfo d in new DirectoryInfo(assetPath).GetDirectories())
            {
                try
                {
                    if (File.Exists(d.FullName + "//assetInfo.xml"))
                    {
                        AssetInfo a = AssetInfo.Load(d.FullName + "//assetInfo.xml");
                        _assets.Add(a);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
        public SongAsset GetSongAsset(Guid g)
        {
            SongAsset a=null;
            if (File.Exists(assetPath + "//" + g.ToString() + "//song.xml"))
            {
                a=SongAsset.Load(assetPath + "//" + g.ToString() + "//song.xml");
                
            }
            return a;
        }
        AssetInfoList _assets;

        public bool AssetsSongExists(Guid id, Guid version)
        {
            foreach (AssetInfo i in _assets)
            {
                if ((i.Id == id) && (i.Version == version))
                    return true;
            }
            return false;
        }
        public void AssetSongSend(Guid Id, Guid Version, string Name, string songXml)
        {
            RemoveAssetInfo(Id);
            AssetInfo newAsset=SaveSong(Id, Version, Name, songXml);
            _assets.Add(newAsset);
        }
        public void AssetFileSend(Guid Id, Guid Version, string Name, Stream songXml)
        {
            RemoveAssetInfo(Id);
            AssetInfo newAsset = SaveFile(Id, Version, Name, songXml);
            _assets.Add(newAsset);
        }
        private AssetInfo SaveFile(Guid Id, Guid Version, string Name, Stream songXml)
        {
            string savePath = this.assetPath + Id.ToString() + "//";
            if (!Directory.Exists(savePath))
                Directory.CreateDirectory(savePath);

            AssetInfo a = new AssetInfo(Id, Name, Version);
            
            XmlSerializer x = new XmlSerializer(typeof(Asset));
            FileStream stream = new FileStream(savePath + Name, FileMode.Create);

            StreamReader s = new StreamReader(songXml);
            StreamWriter write = new StreamWriter(stream);
            while (!s.EndOfStream)
                write.Write(s.Read());

            XmlSerializer x2 = new XmlSerializer(typeof(AssetInfo));
            FileStream stream2 = new FileStream(savePath + Name +".asset", FileMode.Create);

            x2.Serialize(stream2, a);
            stream.Close();

            return a;
        }
        private AssetInfo SaveSong(Guid Id, Guid Version, string Name, string songXml)
        {
            string savePath = this.assetPath + Id.ToString()+"//";
            if (!Directory.Exists(savePath))
                Directory.CreateDirectory(savePath);

            AssetInfo a = new AssetInfo(Id,Name,Version);

            XmlSerializer x = new XmlSerializer(typeof(Asset));
            FileStream stream = new FileStream(savePath+"song.xml", FileMode.Create);
            Asset asset=new Asset(songXml);
            x.Serialize(stream, asset);
            stream.Close();

            XmlSerializer x2 = new XmlSerializer(typeof(AssetInfo));
            FileStream stream2 = new FileStream(savePath+"assetInfo.xml", FileMode.Create);
            
            x2.Serialize(stream2, a);
            stream.Close();

            return a;
        }
        public void RemoveAssetInfo(Guid Id)
        {
            AssetInfo r=null;
            foreach (AssetInfo a in _assets)
            {
                if (a.Id == Id)
                    r = a;
            }
            if (r!=null)
                _assets.Remove(r);
            
        }
    }
    

    
}
